package com.molyfun.parents.activity.classroom;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.fragment.dialog.CommonDialogFragment;
import com.molyfun.parents.rest.bean.NewSignUpSubmitInfoRequest;
import com.molyfun.parents.rest.bean.NewTeamEvaluateTrainingListResponse;
import com.molyfun.parents.rest.bean.SignUpSubmitInfoResponse;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.uievent.UIEventListener;
import com.molyfun.parents.utils.Const;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NewTeamEvaluateActivity extends BaseActivity implements View.OnClickListener, UIEventListener {

    private String TAG = "SignUpEvalucateSheetActivity";
    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.base_title)
    TextView mTitle;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @InjectView(R.id.submit_button)
    TextView mSubmitButton;
    private NewTeamEvaluateAdapter newTeamEvaluateAdapter;

    private String mGroupId;
    private int mRule;

    public static final void startActivity(Context context, String group_id, int rule) {
        Intent intent = new Intent();
        intent.setClass(context, NewTeamEvaluateActivity.class);
        intent.putExtra(Const.GROUP_ID, group_id);
        intent.putExtra(Const.TEAM_RULE, rule);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_team_sheet);
        ButterKnife.inject(this);
        initView();

        MFPApplication.getInstance().getEventController().addUIEventListener(EventDispatcherEnum.UI_EVENT_NEW_TEAM_SHEET_FINISH, this);

    }

    @Override
    public void handleUIEvent(Message msg) {
        switch (msg.what){
            case EventDispatcherEnum.UI_EVENT_NEW_TEAM_SHEET_FINISH:
                finish();
                break;
        }
    }

    private void initView() {
        getSupportActionBar().setTitle("");
        mTitle.setText("分组评估表");
        mBack.setOnClickListener(this);
        mSubmitButton.setOnClickListener(this);
        mSubmitButton.setText("下一步");

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        newTeamEvaluateAdapter = new NewTeamEvaluateAdapter(this, getSupportFragmentManager());
        mRecyclerView.setAdapter(newTeamEvaluateAdapter);

        mGroupId = getIntent().getStringExtra(Const.GROUP_ID);

        mRule = getIntent().getIntExtra(Const.TEAM_RULE, 0);

        if((mRule & 0x02) == 0){
            mSubmitButton.setText("提交");
        }

        if(!TextUtils.isEmpty(mGroupId)){
            loadData();
        }else{
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        MFPApplication.getInstance().getEventController().removeUIEventListener(EventDispatcherEnum.UI_EVENT_NEW_TEAM_SHEET_FINISH, this);
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;

            case R.id.submit_button:
                if((mRule & 0x02) == 0){
                    submitData();
                }else{
                    NewTeamSheetActivity.startActivity(this, mGroupId, newTeamEvaluateAdapter.getList());
                }
                break;
        }
    }

    private void loadData() {
        showProgressDialog();
        Call<NewTeamEvaluateTrainingListResponse> call = MFPApplication.getInstance().getRestClient().getClassTeamEvaluateTrainingList(mGroupId);
        call.enqueue(new Callback<NewTeamEvaluateTrainingListResponse>() {
            @Override
            public void onResponse(Call<NewTeamEvaluateTrainingListResponse> call, Response<NewTeamEvaluateTrainingListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
//                    List<SignUpEvaluateResponse.DataBean.RecordsBean> records = response.body().getData().getRecords();
                    newTeamEvaluateAdapter.refreshData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<NewTeamEvaluateTrainingListResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    private void submitData() {
        NewSignUpSubmitInfoRequest newSignUpSubmitInfoRequest = new NewSignUpSubmitInfoRequest();
        newSignUpSubmitInfoRequest.setGroupid(mGroupId);
        newSignUpSubmitInfoRequest.setTraining(newTeamEvaluateAdapter.getList());
        showProgressDialog();
        Call<SignUpSubmitInfoResponse> submitInfoCall = MFPApplication.getInstance().getRestClient().newTeamSheetSubmitInfo(newSignUpSubmitInfoRequest);
        submitInfoCall.enqueue(new Callback<SignUpSubmitInfoResponse>() {
            @Override
            public void onResponse(Call<SignUpSubmitInfoResponse> call, Response<SignUpSubmitInfoResponse> response) {
                dismissProgressDialog();
                if (response.body() != null) {
                    if (response.body().getCode() == 200) {
                        new CommonDialogFragment.CommonDialogBuilder("提交成功", "报名成功请等待老师审核，审核通过后可在我的班级中查看分组情况。")
                                .setCommonDialogCallback(new CommonDialogFragment.CommonDialogCallback() {
                                    @Override
                                    public void onCommonDialogClose() {
                                        finish();
                                    }
                                })
                                .build(getSupportFragmentManager().beginTransaction());
                    } else {
                        toast(response.body().getMsg());
                    }
                }else{
                    toast("提交失败");
                }

            }

            @Override
            public void onFailure(Call<SignUpSubmitInfoResponse> call, Throwable t) {
                dismissProgressDialog();
                toast("提交失败");
            }
        });
    }

}

